package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.dao.UecNationdutyDao;
import cn.ffcs.uec.staff.model.UecNationduty;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecNationdutyDao")
@Transactional
public class UecNationdutyDaoImpl extends BaseDaoImpl implements UecNationdutyDao {

	@Override
	public PageInfo queryPageInfoByUecNationduty(UecNationduty uecNationduty, int current, int pageSize) {
		if (null != uecNationduty) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_NATIONDUTY T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			// 判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecNationduty.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}

			if (!StrUtil.isNullOrEmpty(uecNationduty.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecNationduty.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecNationduty.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecNationduty.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecNationduty.class);
		}
		return null;
	}

	@Override
	public UecNationduty queryUecNationdutyById(UecNationduty uecNationduty) {
		if (null != uecNationduty) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_NATIONDUTY T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			// 判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecNationduty.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			if (!StrUtil.isNullOrEmpty(uecNationduty.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecNationduty.getStaffId());
			}
			if (!StrUtil.isNullOrEmpty(uecNationduty.getNationdutyId())) {
				sb.append(" AND T.NATIONDUTY_ID = ? ");
				params.add(uecNationduty.getNationdutyId());
			}
			if (!StrUtil.isNullOrEmpty(uecNationduty.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecNationduty.getPkPsndoc());
			}
			List<UecNationduty> list = this.jdbcFindList(sb.toString(), params, UecNationduty.class);
			if (list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public PageInfo queryPageInfoByUecNationdutyOnApproval(UecNationduty uecNationduty, int current, int pageSize) {
		if (null != uecNationduty) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_NATIONDUTY T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_PROCESS);

			if (!StrUtil.isNullOrEmpty(uecNationduty.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecNationduty.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecNationduty.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecNationduty.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecNationduty.class);
		}
		return null;
	}

	@Override
	public UecNationduty queryUecNationdutyByNationdutyId(
			UecNationduty uecNationduty) {
		if (null != uecNationduty) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_NATIONDUTY T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			
			if (!StrUtil.isNullOrEmpty(uecNationduty.getNationdutyId())) {
				sb.append(" AND T.NATIONDUTY_ID = ? ");
				params.add(uecNationduty.getNationdutyId());
			}
			
			List<UecNationduty> list = this.jdbcFindList(sb.toString(), params, UecNationduty.class);
			if (list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public UecNationduty getUecNationdutyByUecStaffInfo(
			UecOutStaffInfo uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_NATIONDUTY T WHERE 1 = 1 AND T.STATUS_CD = ? AND T.NM_STAFF_TYPE = ?");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
			}
			List<UecNationduty> list = this.jdbcFindList(sb.toString(), params,
					UecNationduty.class);
			if (list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public List<UecNationduty> getUecNationdutyListByPkpsndoc(StaffAndOutStaff uecOutStaffInfo) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_NATIONDUTY T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecOutStaffInfo.getPkPsndoc());
			}
			sb.append(" ORDER BY T.WORKDATE");
			List<UecNationduty> list = this.jdbcFindList(sb.toString(), params, UecNationduty.class);
			if (list.size() > 0) {
				return list;
			}
		}
		return null;
	}
}
